<?php

namespace App\Modules\Admin\Entities\Log;

use App\Models\Model;
use App\Modules\Apicrzs\Entities\Users\UserCartModel;

class GoodsLogModel extends Model
{

    protected $table='goods_log';
    protected $primaryKey='id';
    public $timestamps=false;

    /**
     * 创建各种访问日志
     * @param string $type
     * @param array $data
     * @return bool
     */
    public function createLog(string $type, array $data)
    {
//        if (!in_array($type, ['order', 'pay', 'refund']) && (!isset($data['goods_id']) || !$data['goods_id'])) {
//
//            return false;
//        }
//        if ($type != 'visit' && (!isset($data['user_id']) || !$data['user_id'])) {
//            return false;
//        }
        $log_data = $log_data_all = [];
        $log_data['type'] = $type;
        $log_data['goods_id'] = $data['goods_id'] ?? 0;
        $log_data['user_id'] = $data['user_id'] ?? 0;
        switch ($type) {
            case 'visit'://访问
                $log_data['visit_num'] = isset($data['visit_num']) && $data['visit_num'] ? $data['visit_num'] : 1;
                break;
            case 'cart'://加入购物车
                $log_data['cart_num'] = isset($data['cart_num']) && $data['cart_num'] ? $data['cart_num'] : 1;
                break;
            case 'collect'://收藏
                $log_data['collect_num'] = isset($data['collect_num']) && $data['collect_num'] ? $data['collect_num'] : 1;
                break;
           /* case 'order'://下单
                if (!isset($data['order_id']) || !$data['order_id']) {
                    return false;
                }
                $cartInfo = UserCartModel::getInstance()->getOrderCartInfo($data['order_id']);
                foreach ($cartInfo as $value) {
                    $product = $value['cart_info'];
                    $log_data['goods_id'] = $product['goods_id'] ?? 0;
                    $log_data['order_num'] = $product['cart_num'] ?? 1;
                    $log_data_all[] = $log_data;
                }
                break;
            case 'pay'://支付
                if (!isset($data['order_id']) || !$data['order_id']) {

                }
                $cartInfo = GoodsOrderCartInfoModel::getInstance()->getOrderCartInfo($data['order_id']);
                foreach ($cartInfo as $value) {
                    $product = $value['cart_info'];
                    $log_data['product_id'] = $product['product_id'] ?? 0;
                    $log_data['pay_num'] = $product['cart_num'] ?? 0;
                    $log_data['cost_price'] = $product['costPrice'] ?? 0;
                    $log_data['pay_price'] = $product['truePrice'] ?? 0;
                    $log_data['pay_uid'] = $data['uid'] ?? 0;
                    $log_data_all[] = $log_data;
                }
                break;
            case 'refund'://退款
                if (!isset($data['order_id']) || !$data['order_id']) {

                }
                $cartInfo = GoodsOrderCartInfoModel::getInstance()->getOrderCartInfo($data['order_id']);
                foreach ($cartInfo as $value) {
                    $product = $value['cart_info'];
                    $log_data['product_id'] = $product['product_id'] ?? 0;
                    $log_data['uid'] = $data['uid'] ?? 0;
                    $log_data['pay_uid'] = $data['uid'] ?? 0;
                    $log_data['refund_num'] = $product['cart_num'] ?? 0;
                    $log_data['refund_price'] = $product['truePrice'] ?? 0;
                    $log_data_all[] = $log_data;
                }
                break;*/
            default:
                return false;
        }
        if ($log_data_all) {
            $res = $this->createAll($log_data_all);
        } else {
            $res = $this->create($log_data);
        }
        if (!$res) {
            return false;
        }
        return true;
    }

    public function getRanking()
    {
        return $this->select([
                'product_id',
                'SUM(visit_num) as visit',
                'COUNT(distinct(uid)) as user',
                'SUM(cart_num) as cart',
                'SUM(order_num) as orders',
                'SUM(pay_num) as pay',
                'SUM(pay_price * pay_num) as price',
                'SUM(cost_price) as cost',
                'ROUND((SUM(pay_price)-SUM(cost_price))/SUM(pay_price),2) as profit',
                'SUM(collect_num) as collect',
                'ROUND((COUNT(distinct(pay_uid))-1)/COUNT(distinct(uid)),2) as changes',
                'COUNT(distinct(pay_uid))-1 as repeats'
            ])->group('product_id')->limit(20)->select()->toArray();
    }
    public function getCollectNum($goods_id)
    {
        return $this->where(['goods_id'=>$goods_id,'type'=>'collect'])->sum('collect_num');
    }
}
